# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from ..items import QidianImgsItem


class QidianSpider(CrawlSpider):
    name = 'qidian'
    # allowed_domains = ['qidian.com']
    start_urls = ['https://www.qidian.com/finish?page=1']

    rules = (
        Rule(LinkExtractor(restrict_xpaths="//ul[@class='lbf-pagination-item-list']/li[7]"), follow=True),
        Rule(LinkExtractor(restrict_xpaths="//div[@class='book-mid-info']/h4"), callback="parse_item", )
    )

    def parse_item(self, response):
        item = QidianImgsItem()
        title = response.xpath("//h1/em/text()").extract_first()  # 书名
        author = response.xpath("//a[@class='writer']/text()").extract_first()  # 作者
        b_intro = response.xpath("//p[@class='intro']/text()").extract_first()  # 简介
        info = response.xpath("//div[@class='book-intro']/p/text()").extract()  # 作品信息
        image = response.xpath("//div[@class='book-img']/a/img/@src").extract_first()  # 图片地址
        item['title'] = title
        item['author'] = author
        item['b_intro'] = b_intro
        item['info'] = info
        item['image_urls'] = ['http:' + image]
        yield item
